#include "stdafx.h"
#include "ServerDefines.h"
Global ThisServer;

char* strntok( char *string, const char *end, const char *control, char **next )
{
	unsigned char *str;
	const unsigned char *ctl = (const unsigned char*)control;
	unsigned char map[32];
	int count;

	if( *next == end ) 
		return NULL;

	/* Clear control map */
	for (count = 0; count < 32; count++)
	{
		map[count] = 0;
	}

	/* Set bits in delimiter table */
	do {
		map[*ctl >> 3] |= (1 << (*ctl & 7));
	} while (*ctl++);

	/* If string is NULL, set str to the saved
	* pointer (i.e., continue breaking tokens out of the string
	* from the last strtok call) */
	if (string != NULL )
	{
		str = (unsigned char*)string;
	}
	else
	{
		str = (unsigned char*)(*next+1);
	}

	/* Find beginning of token (skip over leading delimiters). Note that
	* there is no token iff this loop sets str to point to the terminal
	* null (*str == 0) */
	while ((map[*str >> 3] & (1 << (*str & 7))) && *str != 0 && (char*)str != end )
	{
		str++;
	}

	string = (char*)str;

	/* Find the end of the token. If it is not the end of the string,
	* put a null there. */
	for ( ; *str != 0 ; str++ )
	{
		if ( ( (char*)str == end ) || (map[*str >> 3] & (1 << (*str & 7))) )
		{
			//*str++ = 0;
			break;
		}
	}

	/* Update context */
	*next = (char*)str;

	/* Determine if a token has been found. */
	if (string == (char*)str)
	{
		return NULL;
	}
	else
	{
		return string;
	}
}

xgc_void ParseCommandLine( xgc_lpcstr szCommandLine )
{
	xgc_lpcstr cmd_line[2] = { szCommandLine, szCommandLine };
	while( cmd_line[1] = strchr( cmd_line[0], '\"' ) )
		cmd_line[0] = cmd_line[1]+1;

	xgc_lpstr cmd = strdup( cmd_line[0] );
	xgc_char splite[] = ", -/";
	xgc_lpstr end = xgc_nullptr, strend = cmd + strlen(cmd);

	xgc_lpstr begin = strntok( cmd, strend, splite, &end );
	while( begin )
	{
		*end = 0;

		if( stricmp( begin, "kind" ) == 0 || cmd[0] == 'k' )
		{
			begin = strntok( xgc_nullptr, strend, splite, &end );
			*end = 0;

			ThisServer.kind = atoi( begin );
		}
		else if( stricmp( begin, "uuid" ) == 0 || begin[0] == 'u' )
		{
			begin = strntok( xgc_nullptr, strend, splite, &end );
			*end = 0;

			ThisServer.uuid = atoi( begin );
		}
		else if( stricmp( begin, "group" ) == 0 || begin[0] == 'g' )
		{
			begin = strntok( xgc_nullptr, strend, splite, &end );
			*end = 0;

			ThisServer.group = atoi( begin );
		}
		else if( stricmp( begin, "local" ) == 0 || begin[0] == 'l' )
		{
			begin = strntok( xgc_nullptr, strend, splite, &end );
			*end = 0;
			
			xgc_lpstr mid = strchr( begin, ':' );
			if( mid == xgc_nullptr )
				ThisServer.local.port = 0;
			else
				ThisServer.local.port = atoi( mid + 1 );

			*mid = 0;
			strncpy( ThisServer.local.address, begin, mid - begin );
		}
		else if( stricmp( begin, "center" ) == 0 || begin[0] == 'c' )
		{
			begin = strntok( xgc_nullptr, strend, splite, &end );
			*end = 0;

			xgc_lpstr mid = strchr( begin, ':' );
			if( mid == xgc_nullptr )
				ThisServer.center.port = 0;
			else
				ThisServer.center.port = atoi( mid + 1 );

			*mid = 0;
			strncpy( ThisServer.center.address, begin, mid - begin );
		}

		begin = strntok( xgc_nullptr, strend, splite, &end );
	}

	free( cmd );
}
